<?php

namespace maotool\src\test;

use maotool\util\ArrayUtil;
use maotool\util\TreeUtil;
use PHPUnit\Framework\TestCase;

class TreeUtilTest extends TestCase
{

    private $list = [
        ["id" => 1, "name" => "湖北省", "pid" => 0],
        ["id" => 2, "name" => "武汉市", "pid" => 1],
        ["id" => 3, "name" => "襄阳市", "pid" => 1],
        ["id" => 4, "name" => "汉阳区", "pid" => 2],
        ["id" => 5, "name" => "襄州区", "pid" => 3],
        ["id" => 6, "name" => "襄城区", "pid" => 3],
        ["id" => 7, "name" => "樊城区", "pid" => 3],
        ["id" => 8, "name" => "石桥镇", "pid" => 5],
        ["id" => 9, "name" => "黄集镇", "pid" => 5],
        ["id" => 10, "name" => "武昌区", "pid" => 2],
        ["id" => 11, "name" => "江汉区", "pid" => 2],
        ["id" => 12, "name" => "洪山区", "pid" => 2],
        ["id" => 13, "name" => "硚口区", "pid" => 2],
        ["id" => 14, "name" => "江岸区", "pid" => 2],
        ["id" => 15, "name" => "青山区", "pid" => 2],
        ["id" => 16, "name" => "高新区", "pid" => 2],
        ["id" => 17, "name" => "建桥街道", "pid" => 4],
        ["id" => 18, "name" => "晴川街道", "pid" => 4],
        ["id" => 19, "name" => "鹦鹉街道", "pid" => 4],
        ["id" => 20, "name" => "洲头街道", "pid" => 4],
        ["id" => 21, "name" => "五里墩街道", "pid" => 4],
        ["id" => 22, "name" => "琴断口街道", "pid" => 4],
        ["id" => 23, "name" => "江堤街道", "pid" => 4],
        ["id" => 25, "name" => "断层街道", "pid" => 24, "pid_code" => '1,2,4,24,25'],
        ["id" => 26, "name" => "中间断层街道", "pid" => 24, "pid_code" => '1'],
        ["id" => 27, "name" => "顶级断层街道", "pid" => 24],
    ];

    public function testListToTree()
    {
        $config = [
            "id" => "id",
            "name" => "name",
            "pid" => "pid",
            "children" => "children",
            "pid_name" => "pid_name",
            "pid_code" => "pid_code",
            "pid_level" => "pid_level",
        ];
        $tree = TreeUtil::listToTree($this->list, $config);
        $parent = TreeUtil::findTreeByParent($tree, ["id" => 25, "name" => "断层街道", "pid" => 24, "pid_code" => '1,2,4,24,25'], $config);
        $parents = TreeUtil::findTreeByParents($tree, ["id" => 25, "name" => "断层街道", "pid" => 24, "pid_code" => '1,2,4,24,25'], $config);
        echo json_encode($tree);
        $this->assertTrue(true);
    }

    public function testListToTree1()
    {
        $list = json_decode('[
  {
    "value": 50,
    "label": "销售三部",
    "id": 50,
    "pid": 40,
    "name": "销售三部",
    "pid_code": "40,50",
    "pid_name": "坦博总部2025/销售三部",
    "pid_level": "1",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 49,
    "label": "销售一组",
    "id": 49,
    "pid": 45,
    "name": "销售一组",
    "pid_code": "40,41,45,49",
    "pid_name": "坦博总部2025/销售部/销售二部/销售一组",
    "pid_level": "3",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 48,
    "label": "销售二组",
    "id": 48,
    "pid": 44,
    "name": "销售二组",
    "pid_code": "40,41,44,48",
    "pid_name": "坦博总部2025/销售部/销售一部/销售二组",
    "pid_level": "3",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 47,
    "label": "销售一组",
    "id": 47,
    "pid": 44,
    "name": "销售一组",
    "pid_code": "40,41,44,47",
    "pid_name": "坦博总部2025/销售部/销售一部/销售一组",
    "pid_level": "3",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 46,
    "label": "系统维护部",
    "id": 46,
    "pid": 0,
    "name": "系统维护部",
    "pid_code": "46",
    "pid_name": "系统维护部",
    "pid_level": "0",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 45,
    "label": "销售二部",
    "id": 45,
    "pid": 41,
    "name": "销售二部",
    "pid_code": "40,41,45",
    "pid_name": "坦博总部2025/销售部/销售二部",
    "pid_level": "2",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 44,
    "label": "销售一部",
    "id": 44,
    "pid": 41,
    "name": "销售一部",
    "pid_code": "40,41,44",
    "pid_name": "坦博总部2025/销售部/销售一部",
    "pid_level": "2",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 43,
    "label": "运维部",
    "id": 43,
    "pid": 40,
    "name": "运维部",
    "pid_code": "40,43",
    "pid_name": "坦博总部2025/运维部",
    "pid_level": "1",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 42,
    "label": "技术部",
    "id": 42,
    "pid": 40,
    "name": "技术部",
    "pid_code": "40,42",
    "pid_name": "坦博总部2025/技术部",
    "pid_level": "1",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 41,
    "label": "销售部",
    "id": 41,
    "pid": 40,
    "name": "销售部",
    "pid_code": "40,41",
    "pid_name": "坦博总部2025/销售部",
    "pid_level": "1",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 40,
    "label": "坦博总部2025",
    "id": 40,
    "pid": 0,
    "name": "坦博总部2025",
    "pid_code": "40",
    "pid_name": "坦博总部2025",
    "pid_level": "0",
    "posts": [
      {
        "id": 3,
        "name": "总经理",
        "created_time": "2025-01-15 22:44:45",
        "updated_time": "2025-03-05 14:57:54",
        "deleted_time": null
      },
      {
        "id": 5,
        "name": "总裁",
        "created_time": "2025-02-26 17:32:26",
        "updated_time": "2025-03-05 11:11:04",
        "deleted_time": null
      },
      {
        "id": 11,
        "name": "副总经理",
        "created_time": "2025-02-26 17:39:16",
        "updated_time": "2025-02-26 17:59:18",
        "deleted_time": null
      },
      {
        "id": 10,
        "name": "副总裁",
        "created_time": "2025-02-26 17:38:26",
        "updated_time": "2025-02-26 17:59:29",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 39,
    "label": "销售部",
    "id": 39,
    "pid": 38,
    "name": "销售部",
    "pid_code": "38,39",
    "pid_name": "坦博分公司1/销售部",
    "pid_level": "1",
    "posts": [
      {
        "id": 6,
        "name": "部门总监",
        "created_time": "2025-02-26 17:33:10",
        "updated_time": "2025-03-05 14:48:13",
        "deleted_time": null
      },
      {
        "id": 7,
        "name": "部门经理",
        "created_time": "2025-02-26 17:34:53",
        "updated_time": "2025-03-05 14:49:33",
        "deleted_time": null
      },
      {
        "id": 8,
        "name": "部门副经理",
        "created_time": "2025-02-26 17:35:14",
        "updated_time": "2025-02-26 17:35:14",
        "deleted_time": null
      },
      {
        "id": 9,
        "name": "部门副总监",
        "created_time": "2025-02-26 17:37:36",
        "updated_time": "2025-03-05 11:12:10",
        "deleted_time": null
      },
      {
        "id": 13,
        "name": "组长",
        "created_time": "2025-03-05 14:41:50",
        "updated_time": "2025-03-05 14:41:50",
        "deleted_time": null
      }
    ]
  },
  {
    "value": 38,
    "label": "坦博分公司1",
    "id": 38,
    "pid": 0,
    "name": "坦博分公司1",
    "pid_code": "38",
    "pid_name": "坦博分公司1",
    "pid_level": "0",
    "posts": [
      {
        "id": 3,
        "name": "总经理",
        "created_time": "2025-01-15 22:44:45",
        "updated_time": "2025-03-05 14:57:54",
        "deleted_time": null
      },
      {
        "id": 5,
        "name": "总裁",
        "created_time": "2025-02-26 17:32:26",
        "updated_time": "2025-03-05 11:11:04",
        "deleted_time": null
      },
      {
        "id": 12,
        "name": "子公司总经理",
        "created_time": "2025-02-26 17:39:31",
        "updated_time": "2025-03-05 11:12:21",
        "deleted_time": null
      },
      {
        "id": 11,
        "name": "副总经理",
        "created_time": "2025-02-26 17:39:16",
        "updated_time": "2025-02-26 17:59:18",
        "deleted_time": null
      },
      {
        "id": 10,
        "name": "副总裁",
        "created_time": "2025-02-26 17:38:26",
        "updated_time": "2025-02-26 17:59:29",
        "deleted_time": null
      }
    ]
  }
]', true);
        $tree = TreeUtil::listToTree($list, [
            "id" => "id",
            "name" => "name",
            "pid" => "pid",
            "children" => "children",
            "pid_name" => "pid_name",
            "pid_code" => "pid_code",
            "pid_level" => "pid_level",
        ]);
        TreeUtil::findTreeByParent($tree, ["id" => 10]);
        echo json_encode($tree);
        $this->assertTrue(true);
    }

    public function testRecursion()
    {
        $tree = TreeUtil::listToTree($this->list);
        echo json_encode($tree);
//        TreeUtil::recursionEach($tree, function ($item, $node) {
//            var_dump($item);
//        });
        $this->assertTrue(true);
    }

}